package com.yjt.fresh.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyWalletDetail;
import com.yjt.fresh.api.ApiGetWalletDetailList;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanWalletDetail;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AtyMyWalletDetail extends BaseSimpleActivity {
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.rvAppAtyWalletDetail)
    RecyclerView mRv;
    @BindView(R.id.srlAppAtyWalletDetail)
    SwipeRefreshLayout mSrl;
    private AdpRvAtyWalletDetail mAdpWalletDetail;
    private int mNextRequestPage = 1;
    private boolean isLoadMore;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_wallet_detail;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

    }

    @Override
    protected void initEvent() {
        initRv();
        initClick();
        getData();
    }

    private void getData() {
        ApiGetWalletDetailList apiGetWalletDetailList = new ApiGetWalletDetailList(new HttpOnNextListener<List<BeanWalletDetail>>() {


            @Override
            public void onNext(List<BeanWalletDetail> beanWalletDetails) {
                if (isLoadMore) {
                    setData(false, beanWalletDetails);
                } else {
                    setData(true, beanWalletDetails);
                    mSrl.setRefreshing(false);
                }
            }

            @Override
            public void onReLogin() {

            }
        }, mContext, true, false, false);
        Map<String, Object> map = new HashMap<>();
        map.put("currentPage", mNextRequestPage);
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiGetWalletDetailList.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetWalletDetailList);
    }

    private void setData(boolean isRefresh, List<BeanWalletDetail> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpWalletDetail.setNewData(data);
            mAdpWalletDetail.setEnableLoadMore(true);
        } else {
            if (size > 0) {
                mAdpWalletDetail.addData(data);
                mAdpWalletDetail.loadMoreComplete();
            } else {
                mAdpWalletDetail.loadMoreEnd(true);
                mAdpWalletDetail.setEnableLoadMore(false);
            }
        }

        if (size < 20) {
            mAdpWalletDetail.loadMoreEnd(true);
        }
    }

    private void initRv() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        List<BeanWalletDetail> list = new ArrayList<>();
        mAdpWalletDetail = new AdpRvAtyWalletDetail(R.layout.app_recycle_item_wallet_detail, list);
        mRv.setAdapter(mAdpWalletDetail);
        mAdpWalletDetail.bindToRecyclerView(mRv);
        mAdpWalletDetail.setEmptyView(R.layout.view_empty);
    }

    private void initClick() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
            }
        });

        mAdpWalletDetail.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        }, mRv);
    }
}
